package com.ruoyi.cat.service.impl;

import java.math.BigDecimal;
import java.util.Collections;
import java.util.List;

import com.ruoyi.cat.domain.*;
import com.ruoyi.cat.domain.DTO.*;
import com.ruoyi.cat.domain.VO.DataScreeningVO;
import com.ruoyi.cat.mapper.CatActivityTableMapper;
import com.ruoyi.cat.service.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * h5活动Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-12-26
 */
@Service
@Slf4j
public class CatActivityTableServiceImpl implements ICatActivityTableService
{
    @Autowired
    private CatActivityTableMapper catActivityTableMapper;

    @Autowired
    private ICatChancesService catChancesService;



    @Autowired
    private ICatPrizeDrawService iCatPrizeDrawService;

    @Autowired
    private ICatIprecordTableService iCatIprecordTableService;

    @Autowired
    private ICatChanceService iCatChanceService;

    @Autowired
    private ICatWinningRecordsService iCatWinningRecordsService;

    @Autowired
    private ICatLogTableService iCatLatestTaskService;


    /**
     * 查询h5活动
     * 
     * @param id h5活动主键
     * @return h5活动
     */
    @Override
    public CatActivityTable selectCatActivityTableById(Long id)
    {
        CatActivityTable catActivityTable =  catActivityTableMapper.selectCatActivityTableById(id);
        CatChances catChances = new CatChances();
        catChances.setActivityId(id);
        List<CatChances> list =  catChancesService.selectCatChancesList(catChances);
        catActivityTable.setChancesList(list);
        return catActivityTable;
    }

    @Override
    public CatActivityTable selectCatActivityTableByIdTask(Long id) {
        return catActivityTableMapper.selectCatActivityTableByIdTask(id);
    }

    /**
     * 查询h5活动列表
     * 
     * @param catActivityTable h5活动
     * @return h5活动
     */
    @Override
    public List<CatActivityTable> selectCatActivityTableList(CatActivityTable catActivityTable)
    {
        return catActivityTableMapper.selectCatActivityTableList(catActivityTable);
    }

    /**
     * 新增h5活动
     * 
     * @param catActivityTable h5活动
     * @return 结果
     */
    @Override
    public int insertCatActivityTable(CatActivityTable catActivityTable)
    {

        int i =  catActivityTableMapper.insertCatActivityTable(catActivityTable);
        if (!catActivityTable.getChancesList().isEmpty()){
           catActivityTable.getChancesList().forEach(chances -> {
               chances.setActivityId(catActivityTable.getId());
              /* if (chances.getQuantity() !=null){
                   chances.setChance(new BigDecimal(chances.getQuantity()));
               }*/
               catChancesService.insertCatChances(chances);
           });
        }



        return i;
    }

    /**
     * 修改h5活动
     * 
     * @param catActivityTable h5活动
     * @return 结果
     */
    @Override
    public int updateCatActivityTable(CatActivityTable catActivityTable)
    {

        int i =  catActivityTableMapper.updateCatActivityTable(catActivityTable);
        if (!catActivityTable.getChancesList().isEmpty()){
            catActivityTable.getChancesList().forEach(chances -> {

                /*if (chances.getQuantity() !=null){
                    chances.setChance(new BigDecimal(chances.getQuantity()));
                }*/
                log.info("这个  chances  ----- > {}",chances);
                if (chances.getId() != null){
                    catChancesService.updateCatChances(chances);
                }else{
                    chances.setActivityId(catActivityTable.getId());
                    catChancesService.insertCatChances(chances);
                }
            });
        }
        return i;
    }

    @Override
    public int updateCatActivity(CatActivityTable catActivityTable) {
        return   catActivityTableMapper.updateCatActivityTable(catActivityTable);
    }

    /**
     * 批量删除h5活动
     * 
     * @param ids 需要删除的h5活动主键
     * @return 结果
     */
    @Override
    public int deleteCatActivityTableByIds(Long[] ids)
    {

        return catActivityTableMapper.deleteCatActivityTableByIds(ids);
    }

    /**
     * 删除h5活动信息
     * 
     * @param id h5活动主键
     * @return 结果
     */
    @Override
    public int deleteCatActivityTableById(Long id)
    {
        CatChances catChances = new CatChances();
        catChances.setActivityId(id);
        List<CatChances> list =  catChancesService.selectCatChancesList(catChances);
        for (CatChances chances : list){
            catChancesService.deleteCatChancesById(chances.getId());
        }
        return catActivityTableMapper.deleteCatActivityTableById(id);
    }

    @Override
    public  List<ActivityShareDTO> getActivityListApiShare() {
        return catActivityTableMapper.getActivityListApiShare();
    }

    @Override
    public ActivityTableDTO getActivityTableById(Long id,Long userId) {
        ActivityTableDTO activityTableDTO =  catActivityTableMapper.getActivityTableById(id);
        CatChances catChances = new CatChances();
        catChances.setActivityId(id);
        List<CatChances> list =  catChancesService.selectCatChancesList(catChances);
        activityTableDTO.setChancesList(list);
        DataScreeningVO dataScreeningVO = new DataScreeningVO();
        dataScreeningVO.setActivityId(id);
        dataScreeningVO.setType("2");
        activityTableDTO.setBrowsed(iCatIprecordTableService.getIprecordTableCount(dataScreeningVO));
        CatChance catChance = new CatChance();
        catChance.setArticleId(id);
        //catChance.setUserId(userId);
      //  catChance.setReferenceId(userId);
        activityTableDTO.setParticipated(iCatChanceService.selectCatChanceList(catChance).size());
        CatWinningRecords catWinningRecords = new CatWinningRecords();
        catWinningRecords.setActivityId(id);
        activityTableDTO.setReceivedAlready(iCatWinningRecordsService.selectCatWinningRecordsList(catWinningRecords).size());
        catChance.setUserId(userId);
        if (userId!=null){
            List<CatChance> chanceList =iCatChanceService.selectCatChanceList(catChance);
            if (!list.isEmpty()){
                activityTableDTO.setRemainingTimes(chanceList.get(0).getRemainingChances());
            }
        }else{
            activityTableDTO.setRemainingTimes(activityTableDTO.getFirstChance());
        }
        Long userby = iCatChanceService.getChanceDataByUserIdAndUserId(id,userId);
        if (userby == 0) {
            CatChance catChanceDTo = new CatChance();
            catChanceDTo.setUserId(userId);
            catChanceDTo.setArticleId(id);
            catChanceDTo.setRemainingChances(activityTableDTO.getFirstChance());
            activityTableDTO.setRemainingTimes(activityTableDTO.getFirstChance());
            iCatChanceService.insertCatChance(catChanceDTo);
        }
        return activityTableDTO;
    }

    @Override
    public ActivityTableDTO getActivityTableByMyId(Long id,Long userId) {
        ActivityTableDTO activityTableDTO =  catActivityTableMapper.getActivityTableById(id);
        CatChances catChances = new CatChances();
        catChances.setActivityId(id);
        List<CatChances> list =  catChancesService.selectCatChancesList(catChances);
        if(userId!=null){
            CatPrizeDraw catPrizeDraw = new CatPrizeDraw();
            catPrizeDraw.setArticleId(id);
            catPrizeDraw.setUserId(userId);
            activityTableDTO.setMyDTOList(iCatPrizeDrawService.getDrawMyByUserId(catPrizeDraw));
        }
        activityTableDTO.setChancesList(list);
        DataScreeningVO dataScreeningVO = new DataScreeningVO();
        dataScreeningVO.setActivityId(id);
        dataScreeningVO.setType("2");
        activityTableDTO.setBrowsed(iCatIprecordTableService.getIprecordTableCount(dataScreeningVO));
        CatChance catChance = new CatChance();
        catChance.setArticleId(id);
       // catChance.setUserId(userId);
       // catChance.setReferenceId(userId);
        activityTableDTO.setParticipated(iCatChanceService.selectCatChanceList(catChance).size());
        CatWinningRecords catWinningRecords = new CatWinningRecords();
        catWinningRecords.setActivityId(id);
        activityTableDTO.setReceivedAlready(iCatWinningRecordsService.selectCatWinningRecordsList(catWinningRecords).size());
        catChance.setUserId(userId);
        if (userId!=null){
            catChance.setUserId(userId);
            List<CatChance> chanceList =iCatChanceService.selectCatChanceList(catChance);
            if (!chanceList.isEmpty()){
                activityTableDTO.setRemainingTimes(chanceList.get(0).getRemainingChances());
            }else{
                activityTableDTO.setRemainingTimes(activityTableDTO.getFirstChance());
            }
        }else{
            activityTableDTO.setRemainingTimes(activityTableDTO.getFirstChance());
        }
        if (userId!=null){
            Long userby = iCatChanceService.getChanceDataByUserIdAndUserId(id,userId);
            if ( userby == 0) {
                CatChance catChanceDTo = new CatChance();
                catChanceDTo.setUserId(userId);
                catChanceDTo.setArticleId(id);
                catChanceDTo.setRemainingChances(activityTableDTO.getFirstChance());
                activityTableDTO.setRemainingTimes(activityTableDTO.getFirstChance());
                iCatChanceService.insertCatChance(catChanceDTo);
            }
        }

        AdDataVO adDataVO = new AdDataVO();
        adDataVO.setActivityId(id);
        adDataVO.setUserId(userId);
        List<AdDataVO> catAdData = iCatLatestTaskService.selectCatAdData(adDataVO);
        if (catAdData.size()>0){
            activityTableDTO.setAdState(1L);
        }else {
            activityTableDTO.setAdState(0L);
        }
        return activityTableDTO;
    }

    @Override
    public ActivityTableDTO getLotteryInfo(Long id, Long userId) {
        ActivityTableDTO activityTableDTO =  catActivityTableMapper.getActivityTableById(id);
        CatChance catChance = new CatChance();
        catChance.setArticleId(id);

        DataScreeningVO dataScreeningVO = new DataScreeningVO();
        dataScreeningVO.setActivityId(id);
        dataScreeningVO.setType("2");
        activityTableDTO.setBrowsed(iCatIprecordTableService.getIprecordTableCount(dataScreeningVO));
        activityTableDTO.setParticipated(iCatChanceService.selectCatChanceList(catChance).size());
        CatWinningRecords catWinningRecords = new CatWinningRecords();
        catWinningRecords.setActivityId(id);
        activityTableDTO.setReceivedAlready(iCatWinningRecordsService.selectCatWinningRecordsList(catWinningRecords).size());
        if(userId!=null){
            CatPrizeDraw catPrizeDraw = new CatPrizeDraw();
            catPrizeDraw.setArticleId(id);
            catPrizeDraw.setUserId(userId);
            activityTableDTO.setMyDTOList(iCatPrizeDrawService.getDrawMyByUserId(catPrizeDraw));

            catChance.setUserId(userId);
            List<CatChance> chanceList =iCatChanceService.selectCatChanceList(catChance);
            if (!chanceList.isEmpty()){
                activityTableDTO.setRemainingTimes(chanceList.get(0).getRemainingChances());
            }else{
                activityTableDTO.setRemainingTimes(activityTableDTO.getFirstChance());
            }
        }else{
            activityTableDTO.setRemainingTimes(activityTableDTO.getFirstChance());
        }
        return activityTableDTO;
    }

    @Override
    public List<CatActivityTable> getActivityTask() {
        return catActivityTableMapper.getActivityTask();
    }

    @Override
    public CatActivityTable getDetails(Long id) {
        CatActivityTable catActivityTable =  catActivityTableMapper.selectCatActivityTableById(id);
        CatChances catChances = new CatChances();
        catChances.setActivityId(id);
        List<CatChances> list =  catChancesService.selectCatChancesList(catChances);
        catActivityTable.setChancesList(list);
        //参与人数
        catActivityTable.setParticipate(iCatChanceService.getChanceDataByUserId(id,null));
        DataScreeningVO query = new DataScreeningVO();
        query.setActivityId(id);
        query.setType("2");
        //访问人数
        catActivityTable.setVisitor(iCatIprecordTableService.getIprecordTableCount(query));
        log.info("中奖人数-----"+iCatWinningRecordsService.getWinningRecordsCount(id));
        //中奖人数
        catActivityTable.setWinningLottery(iCatWinningRecordsService.getWinningRecordsCount(id));
        CatWinningRecords catWinningRecords = new CatWinningRecords();
        catWinningRecords.setActivityId(id);
        catWinningRecords.setWhether("1");
        catActivityTable.setCatWinningRecords(iCatWinningRecordsService.selectCatWinningRecordsList(catWinningRecords));
        return catActivityTable;
    }

    @Override
    public CatActivityTable selectCatActivityTableById1(Long id) {
        CatActivityTable catActivityTable =  catActivityTableMapper.selectCatActivityTableById(id);
        CatChances catChances = new CatChances();
        catChances.setActivityId(id);
        List<CatChances> list =  catChancesService.selectCatChancesList(catChances);
        catActivityTable.setChancesList(list);
        return catActivityTable;
    }

    @Override
    public List<CatActivityTable> getActivityDataTime() {
        return catActivityTableMapper.getActivityDataTime();
    }

    @Override
    public List<WriteDTO> getWriteList(Long userId) {
        return catActivityTableMapper.getWriteList(userId);
    }

    @Override
    public List<CatActivityTable> getActivityTaskTask() {
        return catActivityTableMapper.getActivityTaskTask();
    }

    @Override
    public List<LatestTaskDTO> getActivityTaskHome() {
        return catActivityTableMapper.getActivityTaskHome();
    }
}
